package com.itlaobing.test;

public class ThreadTest3 {

    private static class MyThread extends Thread{//继承Thread
        private boolean stopFlag=false;

        public void setStopFlag(boolean stopFlag) {
            this.stopFlag = stopFlag;
        }

        @Override
        public void run() {
//            线程体（需要新创建的线程中执行的任务，可以写在这里）
            for (int i = 0; i <1000 ; i++) {
//                if(stopFlag) break;
                if(isInterrupted()) break;
                System.out.println("副线程"+getName()+"执行："+i);
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        MyThread myThread=new MyThread();
        myThread.setName("线程1");
        System.out.println(myThread.getState());
        myThread.setPriority(Thread.NORM_PRIORITY+1);
        myThread.start();

        for (int i = 0; i <1000 ; i++) {
            System.out.println("主线程执行："+i);
            if(i==500)
//                myThread.join();//myThread.run()
//                myThread.stop();//myThread.run()
//                myThread.setStopFlag(true);
                myThread.interrupt();
        }

        System.out.println(myThread.getState());
    }
}
